from django.db import models

# Receive the pre_delete signal and delete the file associated with the model instance.
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver


# Create your models here.
class Hotel(models.Model):
	"""docstring for Hotel"""
	id = models.AutoField(primary_key=True)
	name = models.CharField(max_length=150, unique=True)
	city = models.CharField(max_length=50)
	country = models.CharField(max_length=50)
	
	def __unicode__(self):
		return '%s %s %s' % (self.name, self.city, self.country)


class HotelPics(models.Model):
	id = models.AutoField(primary_key=True)
	hotel_id = models.ForeignKey(Hotel)
	title = models.CharField(max_length=50)
	description = models.CharField(max_length=200)
	image = models.ImageField(upload_to = 'hotels_pics/', default = 'no-img.jpg')

	def __unicode__(self):
		return '%s %s %s' % (self.title, self.description, self.image.name)



@receiver(pre_delete, sender=HotelPics)
def hotelPics_delete(sender, instance, **kwargs):
	# Pass false so FileField doesn't save the model.
	instance.image.delete(False)
